'''
Mock Tests.
'''
import unittest
from processors.main_processor import MainProcessor
from ut_settings import Settings
from mails.notsending_mail_sender import NotSendingMailSender
import setup
from mails.mail_processor import MailProcessor
from mock_processors import MockScannerFactory
from processors.changeinfo import ChangeInfo
from datetime import datetime

class MockTest(unittest.TestCase):
    """
    Tests for mocks.
    """
    def setUp(self):
        self._repo1 = []
        olddir, tmpdir, \
          projdir = setup.clean_up() #@UnusedVariable pylint: disable-msg=W0612 
        self._settings = Settings()
        self._mail_sender = NotSendingMailSender()
        self._factory = MockScannerFactory(self._settings, tmpdir, olddir)
        self._parser = MainProcessor(self._settings, self._repo1, \
                        MailProcessor(self._settings, self._mail_sender), \
                        self._factory)
  
    def tearDown(self):
        pass


    def testSimpleMockTest(self):
        self._repo1[:] = [ { 
                   "title" : "repo1",
                   "location" : "location",
                   "type" : "test",
                   "changes" :[
                  ChangeInfo("1", "1", "jarek", "jarek.potiuk@mail.com", \
                             datetime.now(), "/x /y", "coment", "http://url"),
                              ],
                   }]        
        self._parser.parse_and_process()
        mails = self._mail_sender.get_sent_mails()
        assert len(mails) == 2, "Wrong number of mails " + \
                    str(len(mails)) + str(mails)
        assert "all@mail.com" in mails[0]['email'], \
            "Should be sent to all %s" % str (mails[0]['email'])
        assert "jarek.potiuk@mail.com" in mails[1]['email'], \
            "Should be sent to jarek.potiuk %s" % str (mails[0]['email'])
        assert len(mails[0]['processors']) == 1
        assert len(mails[0]['processors'][0].changes) == 1
        assert len(mails[0]['email']) == 1
        assert len(mails[1]['processors']) == 1
        assert len(mails[1]['processors'][0].changes) == 1
        assert len(mails[1]['email']) == 1

    def testRepeatedSendsNoChangesTest(self):
        self._repo1[:] = [ { 
                   "title" : "repo1",
                   "location" : "location",
                   "type" : "test",
                   "changes" :[
                  ChangeInfo("1", "1", "jarek", "jarek.potiuk@mail.com", \
                             datetime.now(), "/x /y", "coment", "http://url"),
                              ],
                   }]        
        self._parser.parse_and_process()
        mails = self._mail_sender.get_sent_mails()
        assert len(mails) == 2, "Wrong number of mails " + \
                     str(len(mails)) + str(mails)
        assert len(mails[0]['email']) == 1
        self._mail_sender.clear_sent_mails()
        self._parser.parse_and_process()
        mails = self._mail_sender.get_sent_mails()        
        assert len(mails) == 0, "Wrong number of mails " + str(len(mails))

    def testSendOnlyOnceToAll(self):
        self._repo1[:] = [ { 
                   "title" : "repo1",
                   "location" : "location",
                   "type" : "test",
                   "changes" :[
                  ChangeInfo("1", "1", "jarek", "all@mail.com", \
                             datetime.now(), "/x /y", "coment", "http://url"),
                              ],
                   }]        
        self._parser.parse_and_process()
        mails = self._mail_sender.get_sent_mails()
        assert len(mails) == 1, "Wrong number of mails " \
                        + str(len(mails)) + str(mails)
        assert len(mails[0]['email']) == 1

    def testTwoReposMockTest(self):
        self._repo1[:] = [ { 
                   "title" : "repo1",
                   "repos" : [ 
                       {
                          "location" : "location",
                          "type" : "test",
                          "changes" :[
                  ChangeInfo("1", "1", "jarek", "jarek.potiuk@mail.com", \
                             datetime.now(), "/x /y", "coment", "http://url"),
                                  ],
                       },
                       {
                          "location" : "location2",
                          "type" : "test",
                          "changes" :[
                  ChangeInfo("2", "2", "jareka", "jareka.potiuk@mail.com", \
                             datetime.now(), "/x /y", "coment", "http://url"),
                                  ],
                       },
                    ]       
            }]        
        self._parser.parse_and_process()
        mails = self._mail_sender.get_sent_mails()
        assert len(mails) == 3, "Wrong number of mails " \
                + str(len(mails)) + str(mails)
        assert "all@mail.com" in mails[0]['email'], \
            "Should be sent to all %s" % str (mails[0]['email'])
        assert len(mails[0]['email']) == 1
        assert "jarek.potiuk@mail.com" in mails[1]['email'], \
            "Should be sent to jarek.potiuk %s" % str (mails[1]['email']) 
        assert len(mails[1]['email']) == 1
        assert "jareka.potiuk@mail.com" in mails[2]['email'], \
            "Should be sent to jareka.potiuk %s" % str (mails[2]['email'])
        assert len(mails[2]['email']) == 1

    def testTwoReposWithSendToAllMockTest(self):
        self._repo1[:] = [ { 
                   "title" : "repo1",
                   "send_to_all_in_group" : True,
                   "repos" : [ 
                       {
                          "location" : "location",
                          "type" : "test",
                          "changes" :[
                  ChangeInfo("1", "1", "jarek", "jarek.potiuk@mail.com", \
                             datetime.now(), "/x /y", "coment", "http://url"),
                                  ],
                       },
                       {
                          "location" : "location2",
                          "type" : "test",
                          "changes" :[
                  ChangeInfo("2", "2", "jareka", "jareka.potiuk@mail.com",
                             datetime.now(), "/x /y", "coment", "http://url"),
                                  ],
                       },
                    ]       
            }]        
        self._parser.parse_and_process()
        mails = self._mail_sender.get_sent_mails()
        assert len(mails) == 1, "Wrong number of mails " + \
                    str(len(mails)) + str(mails)
        assert "all@mail.com" in mails[0]['email'], \
            "Should be sent to all %s" % str (mails[0]['email'])
        assert "jarek.potiuk@mail.com" in mails[0]['email'], \
            "Should be sent to jarek.potiuk: %s" % str (mails[1]['email'])
        assert "jareka.potiuk@mail.com" in mails[0]['email'], \
            "Should be sent to jareka.potiuk: %s" % str (mails[1]['email'])
        assert len(mails[0]['email']) == 3

    def testSubsequentChangesSentTodifferentEmails(self):
        self._repo1[:] = [ { 
                   "title" : "repo1",
                   "location" : "location",
                   "type" : "test",
                   "changes" :[
              ChangeInfo("1", "1", "jarek", "jarek.potiuk@mail.com", \
                         datetime.now(), "/x /y", "coment", "http://url"),
                              ],
                   }]        
        self._parser.parse_and_process()
        mails = self._mail_sender.get_sent_mails()
        assert len(mails) == 2, "Wrong number of mails " + \
                            str(len(mails)) + str(mails)
        assert len(mails[1]['email']) == 1
        self._mail_sender.clear_sent_mails()
        self._repo1[:] = [ { 
                   "title" : "repo1",
                   "location" : "location",
                   "type" : "test",
                   "changes" :[
                  ChangeInfo("1", "1", "jarek", "jarek.potiuk@mail.com", \
                             datetime.now(), "/x /y", "coment", "http://url"),
                  ChangeInfo("2", "2", "jarekb", "jarekb.potiuk@mail.com", \
                             datetime.now(), "/x /y", "coment", "http://url"),
                              ],
                   }]        
        self._parser.parse_and_process()
        mails = self._mail_sender.get_sent_mails()
        assert len(mails) == 3, "Wrong number of mails " + \
                         str(len(mails)) + str(mails)
        assert "jarekb.potiuk@mail.com" in mails[1]['email'], \
            "Should be sent to jarekb.potiuk: %s" % str (mails[2]['email'])
        assert "jarek.potiuk@mail.com" in mails[2]['email'], \
            "Should be sent to jarek.potiuk: %s" % str (mails[1]['email'])
